package cn.xin.system.controller;

import cn.xin.base.util.AjaxResult;
import cn.xin.base.util.PageList;
import cn.xin.system.domain.Dictionarytype;
import cn.xin.system.query.DictionarytypeQuery;
import cn.xin.system.service.DictionarytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class DictionarytypeController {
    @Autowired
    private DictionarytypeService dictionarytypeService;

    @GetMapping
    public List<Dictionarytype> findAll() {
        return dictionarytypeService.findAll();
    }

    @GetMapping("/{id}")
    public Dictionarytype findById(@PathVariable("id")Long id) {
        return dictionarytypeService.findById(id);
    }


    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Dictionarytype dictionarytype){

        try {
            if (dictionarytype.getId()==null){
                dictionarytypeService.add(dictionarytype);
            }else {
                dictionarytypeService.update(dictionarytype);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
        return AjaxResult.me().success();
    }

    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        dictionarytypeService.delete(id);
        return AjaxResult.me().success();
    }

    @PostMapping()
    public PageList<Dictionarytype> queryByCount(@RequestBody DictionarytypeQuery query){
        return  dictionarytypeService.queryByPage(query);
    }

    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        dictionarytypeService.batchDelete(ids);
        return AjaxResult.me().success();
    }


}
